package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetOneCategory;
import dhm.com.dhmshop.entity.GetShopCats;
import dhm.com.dhmshop.framework.module.type.fragment.ClassIficationFragment;
import dhm.com.dhmshop.framework.module.type.fragment.TypeCategoryChildFragment;
import dhm.com.dhmshop.framework.utils.StringUtil;
import dhm.com.dhmshop.updata.fragment.ShopTypeFragment;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;

public class ShopTypeActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.tab_type)
    VerticalTabLayout typeTab;
    @BindView(R.id.vp_type)
    ViewPager typeVp;
    private PressenterImpl pressenter;
    private ClassIficationVpFgAdapter classificationVpFgAdapter;
    private List<Fragment> fragments;
    private GetShopCats getShopCats;

    @Override
    protected int getLayout() {
        return R.layout.activity_shop_type;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);

        String shop_id = getIntent().getStringExtra("shop_id");

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("shop_id", shop_id);

        pressenter.sendMessage(this, Constant.GetShopCats, map, GetShopCats.class);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        getShopCats = (GetShopCats) data;

        fragments = new ArrayList<>();
        List<GetShopCats.DataBean> data1 = getShopCats.getData();
        for (GetShopCats.DataBean dataBean : data1) {
            fragments.add(new ShopTypeFragment(dataBean));
        }
        typeTab.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {
                return getShopCats.getData().size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {
                return null;
            }


            @Override
            public QTabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public QTabView.TabTitle getTitle(int position) {
                QTabView.TabTitle title = new QTabView.TabTitle.Builder()
                        .setContent(getShopCats.getData().get(position).getCatName())//设置数据   也有设置字体颜色的方法
                        .build();
                return title;
            }

            @Override
            public int getBackground(int position) {
                return R.drawable.button_bg;//选中的背景颜色
            }
        });

        classificationVpFgAdapter = new ClassIficationVpFgAdapter(getSupportFragmentManager());
        typeVp.setAdapter(classificationVpFgAdapter);
        typeTab.setupWithViewPager(typeVp);
        classificationVpFgAdapter.notifyDataSetChanged();

    }

    @Override
    public void fail(String error) {

    }


    class ClassIficationVpFgAdapter extends FragmentPagerAdapter {

        public ClassIficationVpFgAdapter(FragmentManager fm) {
            super(fm);

        }


        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return StringUtil.preventNull(getShopCats.getData().get(position).getCatName());
        }
    }

}
